﻿using Microsoft.Xna.Framework;
using RaptorEngine;
using RaptorEngine.Components;
using Microsoft.Xna.Framework.Input;
using RaptorEngine.Components.CollidableComponent;
using RaptorEngine.Managers;
using OilRigSumoWrestlers.Scenes.Menu;

namespace OilRigSumoWrestlers.Controllables
{
    public class PlayerController : Controllable
    {
        private Movable _movable;
        private Collidable _collidable;
        private LogicalPlayerIndex _player;

        public PlayerController(Entity entity, LogicalPlayerIndex player)
            : base(entity)
        {
            _player = player;
            if(entity.Components.ContainsKey(ComponentType.Movable))
                _movable = (Movable)entity.Components[ComponentType.Movable];
            if (entity.Components.ContainsKey(ComponentType.Collidable))
                _collidable = (Collidable)entity.Components[ComponentType.Collidable];
        }

        public override void Update(GameTime gameTime)
        {
            ProcessInput();
           
        }

        private void ProcessInput()
        {
            //Enter menu
            if (GameEngine.Instance.Input.CommandActive(GameConstant.MENU + _player))
            {
                GameEngine.Instance.Scene.StopCurrentScene();
                GameEngine.Instance.Scene.PushScene(new PauseMenu(_player));
                GameEngine.Instance.Scene.StartCurrentScene();
                GameEngine.Instance.Scene.ShowCurrentScene();
            }

            //Directional movement
 #if WINDOWS
            float x = 0, y = 0;
            if (GameEngine.Instance.Input.CommandActive(GameConstant.LEFT + _player.ToString()))
            {
                x = -1;
            }
            else if (GameEngine.Instance.Input.CommandActive(GameConstant.RIGHT + _player.ToString()))
            {
                x = 1;
            }

            if (GameEngine.Instance.Input.CommandActive(GameConstant.UP + _player.ToString()))
            {
                y = 1;
            }
            else if (GameEngine.Instance.Input.CommandActive(GameConstant.DOWN + _player.ToString()))
            {
                y = -1;
            }
            if (GameEngine.Instance.Input.GamePadPluggedIn(_player))
            {
#endif          //Move with analog stick
                Move(GameEngine.Instance.Input.GetSticksState(_player).Left);
#if WINDOWS
            }
            else
            {
                Move(new Vector2(x, y));
            }
#endif
            //Push
            if (GameEngine.Instance.Input.CommandActive(GameConstant.PUSH + _player.ToString()))
                Push();

        }

        public void Move(Vector2 input)
        {
            if (input != Vector2.Zero)
            {
                //Set Rotation
                Entity.Rotation = Quaternion.CreateFromRotationMatrix(Matrix.CreateLookAt(Entity.Position, Entity.Position + new Vector3(input.X, 0, input.Y), Vector3.Up));
            }
            _movable.Acceleration = new Vector3(
                input.X * GameConstant.PLAYER_ACCELERATION,
                0, -input.Y * GameConstant.PLAYER_ACCELERATION);
            GameEngine.Instance.Debug.SetData("Rotation", Entity.Rotation.ToString());
        }

        public void Push()
        {
            _collidable.BoundingVolumes["push"].IsActive = true;
        }
    }
}
